package com.wealth.scsj.b_app.view.activity.payment;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.fastjson.JSONObject;
import com.vondear.rxtools.RxSPTool;
import com.wealth.scsj.b_app.R;
import com.wealth.scsj.b_app.base.RetrievePayPwdBase;
import com.wealth.scsj.b_app.bean.BaseBean;
import com.wealth.scsj.b_app.config.RxSPConfig;
import com.wealth.scsj.b_app.presenter.RetrievePayPwdPresenter;
import com.wealth.scsj.b_app.utils.RxCountDown;
import com.wealth.scsj.b_app.utils.SkipUtils;
import com.wealth.scsj.b_app.view.activity.BaseActivity;

import es.dmoral.toasty.Toasty;
import okhttp3.RequestBody;
import rx.Subscriber;
import rx.functions.Action0;

/**
 * 找回支付密码
 */
public class NewRetrievePasswordActivity extends BaseActivity implements View.OnClickListener, RetrievePayPwdBase.IView {

    /**
     * 请输入手机号
     */
    private EditText mPhoneEt;
    /**
     * 获取验证码
     */
    private Button mSendCodeBtn;
    /**  */
    private EditText mVerificationCodeEt;
    /**
     * 验证
     */
    private Button mRetrievePaymentPasswordBtn;
    private RetrievePayPwdPresenter presenter;
    private String userPhone;
    private String code;

    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_retrieve_payment_password);
        //ScreenAdapterTools.getInstance().loadView((ViewGroup) getWindow().getDecorView());
        //ScreenAdapterTools.getInstance().reset(this);//如果希望android7.0分屏也适配的话,加上这句
        initToolbar();
        setToolbarTitle("找回支付密码");
        hasBack(true);
        presenter = new RetrievePayPwdPresenter(this);
        userPhone = RxSPTool.getString(this, RxSPConfig.USER_PHONE);

    }

    @Override
    protected void initView() {

        mPhoneEt = (EditText) findViewById(R.id.phone_et);
        mSendCodeBtn = (Button) findViewById(R.id.send_code_btn);
        mSendCodeBtn.setOnClickListener(this);
        mVerificationCodeEt = (EditText) findViewById(R.id.verification_code_et);
        mRetrievePaymentPasswordBtn = (Button) findViewById(R.id.retrieve_payment_password_btn);
        mRetrievePaymentPasswordBtn.setOnClickListener(this);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.send_code_btn:
                if(mPhoneEt.getText() == null || !mPhoneEt.getText().toString().trim().equals(userPhone) ){
                    Toasty.info(this,"请输入当前登录的手机号");
                    return;
                }
                JSONObject object1 = new JSONObject();
                RequestBody body = RequestBody.create(okhttp3.MediaType
                        .parse("application/json;charset=utf-8"), object1.toString());
                presenter.deliverSendSmsCodeData(body);
                break;
            case R.id.retrieve_payment_password_btn:
                JSONObject object = new JSONObject();
                object.put("phone", userPhone);
//                object.put("templateCode",userPhone);
                code = mVerificationCodeEt.getText().toString().trim();
                object.put("code", code);
                RequestBody requestBody = RequestBody.create(okhttp3.MediaType
                        .parse("application/json;charset=utf-8"), object.toString());
                presenter.deliverVerifySmsCodeData(requestBody);

                break;
            default:
                break;
        }
    }

    @Override
    public void sendSmsCodeSuccessMsg(String s, BaseBean addDABean) {
        Toasty.success(this,s).show();
        RxCountDown.countdown(60)
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        mSendCodeBtn.setClickable(false);
                    }
                })
                .subscribe(new Subscriber<Integer>() {
                    @Override
                    public void onCompleted() {
                        mSendCodeBtn.setClickable(true);
                        mSendCodeBtn.setText("获取验证码");

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Integer integer) {
                        mSendCodeBtn.setText(integer + "S");
                    }
                });
    }

    @Override
    public void verifySmsCodeSuccessMsg(String s, BaseBean deleteDABean) {
        SkipUtils.toResetPaymentPwdWithCode(this,code);
    }

    @Override
    public void SetNewPayPwdSuccessMsg(String s, BaseBean deleteDABean) {

    }

    @Override
    public void showErrorMsg(String s) {
        Toasty.error(this,s).show();
    }
}
